package com.cn.why.film.filmindex.face.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 双重锁的单例模式，默认懒汉式
 */
public class SingleByDoubleCheck {
    private static SingleByDoubleCheck stance = null;
    //构造器变为私有的（除了自己不能实例化）
    private SingleByDoubleCheck(){}

    //synchronized代码块实现
    private static SingleByDoubleCheck getInstanceBySyn(){
        if(stance == null){
            //加锁
            synchronized (SingleByDoubleCheck.class){
                if (stance == null){
                    stance = new SingleByDoubleCheck();
                }
            }
        }
        return stance;
    }

    static ReentrantLock lock = new ReentrantLock();
    //可重入锁lock实现
    private static  SingleByDoubleCheck getInstanceByLock(){
        lock.lock();
        if(stance == null){
            lock.lock();
            if(stance == null){
                stance = new SingleByDoubleCheck();
            }
            lock.unlock();
        }
        lock.unlock();
        return stance;
    }
}
